/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.controller.api;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.framework.exception.YYException;
import com.yyfax.framework.model.YYFaxRsp;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.constants.PayConstants;
import com.yyfax.pay.common.enums.PayRspCodeEnum;
import com.yyfax.pay.config.data.service.MerchantConfigService;
import com.yyfax.pay.order.model.to.bank.QueryBankQuotaListContent;
import com.yyfax.pay.order.model.to.cardbin.CardInfoContent;
import com.yyfax.pay.order.model.to.cardbin.QueryCardBinApiParams;
import com.yyfax.pay.order.service.PayCommonService;
import com.yyfax.pay.order.service.RouteService;
import com.yyfax.pay.third.model.to.yyfax.QueryCardBinContent;

/**
 * 针对友金所内部提供的接口
 * 
 * @author jiangmy
 * @date 2016-08-15 11:54:25
 * @since v1.0.0
 */
@RestController
public class PayCommonApiController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private PayCommonService payCommonService;

	@Autowired
	private MerchantConfigService merchantConfigService;

	@Autowired
	private RouteService routeSerivice;

	@Autowired
	private PayConstants payConstants;

	/**
	 * 查询卡bin,内网使用
	 * 
	 * @author buyi
	 * @date 2017-04-13 16:20:26
	 * @since v1.0.0
	 * @param merchantCode
	 * @param cardNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/common/api/card_bin/query")
	public YYFaxRsp<CardInfoContent> queryCardBin(QueryCardBinApiParams params) throws Exception {
		params.validate();
		// 验签
		params.checkSign(payConstants.getSignKey());

		QueryCardBinContent content = payCommonService.queryCardBin(params.getMerchantCode(), params.getCardNo());
		if (content == null) {
			logger.debug("查询卡bin返回null ");
			return new YYFaxRsp<CardInfoContent>(PayRspCodeEnum.PAY_CODE_QUERY_CARD_BIN_ERROR, "");
		}

		CardInfoContent cardInfoContent = new CardInfoContent(content.getBankCode(), content.getBankName());

		return new YYFaxRsp<CardInfoContent>(PayRspCodeEnum.fromValue(content.getResultCode()), cardInfoContent);
	}

	/**
	 * 获取商户的支持银行
	 * 
	 * @author buyi
	 * @date 2017-05-02 15:56:29
	 * @since v1.0.0
	 * @param merchantCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/common/api/bank_quota/query_list")
	public YYFaxRsp<QueryBankQuotaListContent> queryBankQuotaList(String merchantCode) throws Exception {
		if (StringUtils.isBlank(merchantCode)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "merchantCode 不能为空");
		}

		QueryBankQuotaListContent content = payCommonService.QueryBankQuotaList(merchantCode);

		return new YYFaxRsp<QueryBankQuotaListContent>(PayRspCodeEnum.SUCCESS, content);
	}

//	@RequestMapping(method = RequestMethod.POST, path = "/common/api/card_bin/direct/query")
//	public YYFaxRsp<CardInfoContent> directQueryCardBin(@RequestBody YYPayApiReq<QueryCardBinApiDirectParams> req)
//			throws Exception {
//
//		if (req == null || req.getParams() == null) {
//			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID);
//		}
//
//		QueryCardBinApiDirectParams params = req.getParams();
//
//		// 1.校验参数
//		params.validate();
//
//		// 2.验签
//		MerchantConfigPO merchantConfigPO = merchantConfigService.queryByMerchantCode(params.getMerchantCode());
//		if (merchantConfigPO == null) {
//			throw new YYException(PayRspCodeEnum.PAY_CODE_MERCHANT_NOT_EXISTS, "无效商户：" + params.getMerchantCode());
//		}
//
//		req.checkSign(merchantConfigPO.getSignKey());
//
//		QueryCardBinContent content = payCommonService.queryCardBin(params.getMerchantCode(), params.getCardNo());
//
//		if (content == null) {
//			logger.debug("查询卡bin返回null ");
//			return new YYFaxRsp<CardInfoContent>(PayRspCodeEnum.PAY_CODE_QUERY_CARD_BIN_ERROR, "");
//		}
//
//		CardInfoContent cardInfoContent = new CardInfoContent(content.getBankCode(), content.getBankName());
//
//		return new YYFaxRsp<CardInfoContent>(PayRspCodeEnum.fromValue(content.getResultCode()), cardInfoContent);
//
//	}

	// @RequestMapping(method = RequestMethod.POST, path = "/common/api/route")
	// public YYFaxRsp<RouteContent> route(@RequestBody
	// YYPayApiReq<RouteApiParams> req) throws Exception {
	// if (req == null || req.getParams() == null) {
	// throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID);
	// }
	//
	// RouteApiParams params = req.getParams();
	//
	// // 1.校验参数
	// params.validate();
	//
	// // 2.验签
	// MerchantConfigPO merchantConfigPO =
	// merchantConfigService.queryByMerchantCode(params.getMerchantCode());
	// if (merchantConfigPO == null) {
	// throw new YYException(PayRspCodeEnum.PAY_CODE_MERCHANT_NOT_EXISTS,
	// "无效商户：" + params.getMerchantCode());
	// }
	//
	// req.checkSign(merchantConfigPO.getSignKey());
	//
	// QueryCardBinContent content =
	// payCommonService.queryCardBin(params.getMerchantCode(),
	// params.getCardNo());
	//
	// if (content == null) {
	// logger.debug("查询卡bin返回null ");
	// return new
	// YYFaxRsp<RouteContent>(PayRspCodeEnum.PAY_CODE_QUERY_CARD_BIN_ERROR,
	// "银行卡查询异常");
	// }
	//
	// PaymentEnum paymentEnum = null;
	//
	// if (PayTypeEnum.PAY.isValue(params.getPayType())) {// 如果是支付类型
	// paymentEnum = routeSerivice.routePay(params.getMerchantCode(),
	// content.getBankCode(), null, PayModeEnum.AUTH_PAY.getValue(),
	// ApiTypeEnum.API.getValue());
	// } else if (PayTypeEnum.AUTH.isValue(params.getPayType())) { // 如果是独立鉴权类型
	// paymentEnum = routeSerivice.routeAuthentication(params.getMerchantCode(),
	// null);
	// } else {
	// throw new YYException(PayRspCodeEnum.PAY_CODE_ILLEGAL_PAYTYPE);
	// }
	//
	// if (paymentEnum == null) {
	// throw new YYException(PayRspCodeEnum.COMMON_ERROR_OBJECT_NOT_FOUND,
	// "没有找到相应支付路由信息");
	// }
	//
	// return new YYFaxRsp<RouteContent>(PayRspCodeEnum.SUCCESS, new
	// RouteContent(paymentEnum.getValue(), paymentEnum.getRemark()));
	// }
}
